<template>
  <div class="custom-body" :style="borderStyle">
    <div v-if="showHeader" class="custom-header" :style="titleStyle">
      <div class="header-body" :style="headerStyle">
        <div v-if="temp.sfsxtb === true" :style="titleFont" class="header-icon">
          <i :class="temp.tb" />
        </div>
        <div v-else class="header-line" />
        <div class="header-title">
          <div
            v-for="(item, index) in columns"
            :key="'item-' + index"
            class="custom-header-li"
            :style="titleFont"
            :class="index === active ? 'header-active' : ''"
            @click="headerClick(item, index)"
            @mouseenter="mouseenter(index)"
            @mouseleave="mouseleave(index)"
          >
            <span>{{ item.label }}</span>
          </div>
        </div>
        <div
          v-if="isMore == true"
          :style="titleFont"
          class="header-icon more"
          @click="callBack"
        >
          更多
        </div>
      </div>
    </div>
    <div class="custom-content">
      <component
        :is="template"
        v-if="template"
        :show-header="false"
        :layout="layout"
        :temp="column"
        @fetch-data="callBack"
        @fetch-entity="callBackEntity"
      />
    </div>
  </div>
</template>
<script>
import { queryEntity, queryGuestEntity } from '@/api/portal/manager/columnTemp';
import { getToken } from '@/utils/auth';
export default {
  name: 'PortalAppTabsTempV1',
  props: {
    temp: {
      type: Object,
      default() {
        return {};
      }
    },
    showHeader: {
      type: Boolean,
      default: true
    },
    layout: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      columns: [],
      active: 0,
      activeId: null,
      template: null,
      column: {},
      isMore: false,
      customStyle: {},
      titleStyle: {},
      bodyStyle: {},
      titleFont: {},
      activeTow: null
    };
  },
  computed: {
    borderStyle() {
      if (this.temp.sfdzqp === true) {
        return {
          position: 'absolute',
          left: '0px',
          right: '0px',
          top: '0px',
          bottom: '0px'
        };
      }
      return { height: this.temp.defaultHeight };
    },
    headerStyle() {
      if (this.temp.sfzdy === true) {
        return {
          backgroundColor: this.temp.backgroundColor,
          color: this.temp.fontColor
        };
      }
      return {};
    }
  },
  watch: {
    layout: {
      handler(newData, oldData) {
        this.customStyle = newData;
        this.titleStyle = {
          backgroundColor: newData.mrlb,
          opacity: newData.tmd
        };
        this.titleFont = { color: newData.lmys };
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    temp: {
      handler(newData, oldData) {
        if (this.showHeader === true) {
          const pzx = JSON.parse(newData.data.pzx);
          this.columns = pzx.data;
          this.activeId = this.columns[this.active].temp;
        } else {
          const pzx = JSON.parse(newData.pzx);
          this.columns = pzx.data;
          this.activeId = this.columns[this.active].temp;
        }
        this.initEntity();
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    headerClick(item, index) {
      this.active = index;
      this.activeId = item.temp;
      this.initEntity();
    },
    initEntity() {
      if (this.activeId !== null) {
        if (getToken()) {
          queryEntity({ id: this.activeId }).then(res => {
            const { data } = res;
            const path = data.mbdl + '/' + data.mbfl + '/' + data.mb;
            this.template = () => import(`@/template/${path}`);
            this.column = data;
            this.isMore = this.columns[this.active].isMore;
            this.column.dkfs = this.columns[this.active].dkfs;
            this.column.path = this.columns[this.active].path;
            this.column.showHeader = true;
            this.column.menuId = this.temp.menu.id;
            this.column.menuName = this.temp.menu.name;
            this.column.columnName = this.columns[this.active].label;
          });
        } else {
          queryGuestEntity({ id: this.activeId }).then(res => {
            const { data } = res;
            const path = data.mbdl + '/' + data.mbfl + '/' + data.mb;
            this.template = () => import(`@/template/${path}`);
            this.column = data;
            this.isMore = this.columns[this.active].isMore;
            this.column.dkfs = this.columns[this.active].dkfs;
            this.column.path = this.columns[this.active].path;
            this.column.showHeader = true;
            this.column.menuId = this.temp.menu.id;
            this.column.menuName = this.temp.menu.name;
            this.column.columnName = this.columns[this.active].label;
          });
        }
      }
    },
    callBackEntity(data) {
      data.column = this.column;
      this.$emit('fetch-entity', data);
    },
    callBack() {
      if (this.column.dkfs === 2) {
        // 自定义打开方式
        if (undefined !== this.column.path && this.column.path !== null) {
          window.open(this.column.path, '_blank');
        }
      } else if (this.column.dkfs === 1) {
        this.$emit('fetch-data', this.column);
      } else {
        console.log('');
      }
    },
    mouseenter(index) {
      console.log(1);
      // this.activeTow = index
    },
    mouseleave(index) {
      console.log(2);
      // this.activeTow = null
    }
  }
};
</script>

<style scoped>
.custom-body {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  flex-flow: column nowrap;
  background-color: #ffffff;
  padding: 0px 0px 5px 0px;
  box-sizing: border-box;
  overflow: hidden;
}
.custom-header {
  width: 100%;
  height: 40px;
  flex: 0 1 auto;
  padding: 0px 10px;
  box-sizing: border-box;
  line-height: 40px;
  cursor: pointer;
}
.header-body {
  width: 100%;
  height: 41px;
  border-bottom: 1px solid #eeeeee;
  display: flex;
  flex-flow: row nowrap;
}
.header-line {
  position: absolute;
  left: 0px;
  top: 0px;
  height: 40px;
  width: 4px;
  background-color: #0a84ff;
}
.header-icon {
  width: 20px;
  height: 40px;
  flex: 0 1 auto;
  text-align: center;
  color: #409eff;
}
.header-icon.more {
  width: 40px;
}
.title-span {
  height: 40px;
  padding: 12px 5px;
  box-sizing: border-box;
  line-height: 40px;
  text-align: center;
  position: relative;
}
.title-span::before {
  position: absolute;
  content: "";
  height: 2px;
  bottom: 0px;
  left: 0px;
  right: 0px;
  background-color: #409eff;
}
.header-title {
  height: 50px;
  flex: 1 1 auto;
  display: flex;
  flex-flow: row nowrap;
}
.custom-header-li {
  padding: 0px 10px;
  box-sizing: border-box;
  flex: 0 1 auto;
  height: 40px;
  position: relative;
}
.custom-header-l span {
  display: block;
  height: 100%;
  position: relative;
}
.custom-header-li:hover span {
  color: #409eff;
}
.header-active {
  color: #409eff;
  font-weight: bolder;
}
.header-active::after {
  content: "";
  height: 2px;
  left: 5px;
  right: 5px;
  bottom: 0px;
  position: absolute;
  background-color: #409eff;
}
.custom-content {
  width: 100%;
  height: auto;
  flex: 1 1 auto;
  overflow: hidden;
  box-sizing: border-box;
}
</style>
